package com.bbcare.nutritious.plat.service.imp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.SeqUtil;
import com.bbcare.nutritious.plat.entity.PtBloodsugarRecord;
import com.bbcare.nutritious.plat.service.INewBloodSugarService;
import com.bbcare.nutritious.plat.store.ibatis.INewBloodSugarDao;
import com.bbcare.nutritious.plat.store.ibatis.IWeightDao;

/**
 * 
 * @name:         BloodSugarControllerService.java
 * @author:       tao
 * @createTime:   2017年7月3日
 * @description:  糖尿病  相关服务
 * @modify:
 *
 */
@Service
public class NewBloodSugarService implements INewBloodSugarService {

	public static final Logger logger = Logger.getLogger(NewBloodSugarService.class);

    @Autowired
    private INewBloodSugarDao newBloodSugarDao;
    @Autowired
    private IWeightDao weightDao;
    /**
     * 保存或修改血糖数据
     */
    @Override
    public Map<String, Object> addOrUpdateById(PtBloodsugarRecord bloodsugarRecord) throws Exception {
    	Map<String,Object> rspMap = Collections.synchronizedMap(new HashMap<String,Object>());
    	String tenantId = weightDao.getUserTenantIdByUserId(bloodsugarRecord.getUserId());
    	if(StringUtils.isEmpty(tenantId)){
    		throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST,"患者信息不存在");
    	}
    	bloodsugarRecord.setTenantId(tenantId);
    	String nowTime = DateUtil.getTimeString(DateUtil.getSysDateA());// 当前时间
    	//保存血糖数据,id存在则修改
    	if(StringUtils.isEmpty(bloodsugarRecord.getId())){
    		//判断该天信息是否存在
        	int total = newBloodSugarDao.countByTestDate(bloodsugarRecord);
        	if(total>0){
        		throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST,"新增失败,该时间点数据已存在");
        	}
    		//生成id
        	bloodsugarRecord.setId(SeqUtil.getSeqNo());
        	bloodsugarRecord.setCreateTime(nowTime);//创建时间
        	bloodsugarRecord.setUpdateTime(nowTime);//修改时间
        	newBloodSugarDao.saveBloodsugarData(bloodsugarRecord);
        	//保存历史
        	bloodsugarRecord.setUpdateType(Constants.HANDLE_TYPE_ADD);
        	newBloodSugarDao.saveBloodsugarHis(bloodsugarRecord);
    	}else{
    		bloodsugarRecord.setUpdateTime(nowTime);//修改时间
    		newBloodSugarDao.updateDevieRef(bloodsugarRecord);
    		//保存历史
    		bloodsugarRecord.setUpdateType(Constants.HANDLE_TYPE_CHANGE);
    		newBloodSugarDao.saveBloodsugarHis(bloodsugarRecord);
    	}
    	rspMap.put("bloodsugarRecord", bloodsugarRecord);
		return rspMap;
    }
    
    /**
     * 通过id删除血糖数据
     */
     @Override
 	public Map<String, Object> delBloodsugarRecordById(String id) throws Exception {
     	//创建线程安全的hashmap返回对象
     	Map<String,Object> rspMap = Collections.synchronizedMap(new HashMap<String,Object>());
     	//查询该数据是否存在
     	PtBloodsugarRecord bloodsugarRecord = new PtBloodsugarRecord();
     	bloodsugarRecord.setId(id);
     	List<PtBloodsugarRecord> list = newBloodSugarDao.listBloodsugar(bloodsugarRecord);
     	if(CollectionUtils.isEmpty(list)){
     		throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST,"该条数据已删除,请勿重复操作");
     	}
     	bloodsugarRecord = list.get(0);
     	//删除体重信息
     	newBloodSugarDao.deleteyId(id);
     	//保存删除记录
     	bloodsugarRecord.setUpdateTime(DateUtil.getTimeString(DateUtil.getSysDateA()));
     	bloodsugarRecord.setUpdateType(Constants.HANDLE_TYPE_DELETE);
     	newBloodSugarDao.saveBloodsugarHis(bloodsugarRecord);
 		return rspMap;
 	}
     
     /**
      * 分页查询血糖数据
      * @throws Exception 
      */
 	@Override
 	public Map<String, Object> listbloodsugarForPage(PtBloodsugarRecord bloodsugarRecord) throws Exception {
 		//创建线程安全的hashmap返回对象
     	Map<String,Object> rspMap = Collections.synchronizedMap(new HashMap<String,Object>());
     	//判断该用户是否存在
     	String tenantId = weightDao.getUserTenantIdByUserId(bloodsugarRecord.getUserId());
     	if(StringUtils.isEmpty(tenantId)){
     		throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST,"患者信息不存在");
     	}
     	//分页数据
     	List<PtBloodsugarRecord> list = newBloodSugarDao.listBloodsugarForPage(bloodsugarRecord);
     	//统计数据总量
     	int totalCount = newBloodSugarDao.countBloodsugarForPage(bloodsugarRecord);
     	//处理mysql的timestemp格式问题
     	if(!CollectionUtils.isEmpty(list)){
     		for (int i = 0; i < list.size(); i++) {
     			list.get(i).setCreateTime(DateUtil.getTimeStampSec(list.get(i).getCreateTime()));
     			list.get(i).setUpdateTime(DateUtil.getTimeStampSec(list.get(i).getUpdateTime()));
     		}
     	}
     	rspMap.put("list", list);
     	rspMap.put("totalCount", totalCount);
 		return rspMap;
 	}
 	
 	/**
     * 分页查询血糖数据
     * @throws Exception 
     */
	@Override
	public Map<String, Object> listbloodsugarForPagePC(PtBloodsugarRecord bloodsugarRecord) throws Exception {
		//创建线程安全的hashmap返回对象
    	Map<String,Object> rspMap = Collections.synchronizedMap(new HashMap<String,Object>());
    	//判断该用户是否存在
    	String tenantId = weightDao.getUserTenantIdByUserId(bloodsugarRecord.getUserId());
    	if(StringUtils.isEmpty(tenantId)){
    		throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST,"患者信息不存在");
    	}
    	//分页数据
    	List<PtBloodsugarRecord> list = newBloodSugarDao.listBloodsugarForPagePC(bloodsugarRecord);
    	//统计数据总量
    	int totalCount = newBloodSugarDao.countBloodsugarForPagePC(bloodsugarRecord);
    	//处理mysql的timestemp格式问题
    	if(!CollectionUtils.isEmpty(list)){
    		for (int i = 0; i < list.size(); i++) {
    			list.get(i).setCreateTime(DateUtil.getTimeStampSec(list.get(i).getCreateTime()));
    			list.get(i).setUpdateTime(DateUtil.getTimeStampSec(list.get(i).getUpdateTime()));
    		}
    	}
        rspMap.put("success", true);
    	rspMap.put("rows", list);
    	rspMap.put("total", totalCount);
		return rspMap;
	}
 	/**
 	 * 通过userid查询血糖数据
 	 */
 	@Override
 	public Map<String, Object> listbloodsugar(PtBloodsugarRecord bloodsugarRecord) throws Exception {
 		//创建线程安全的hashmap返回对象
     	Map<String,Object> rspMap = Collections.synchronizedMap(new HashMap<String,Object>());
     	//判断该用户是否存在
     	String tenantId = weightDao.getUserTenantIdByUserId(bloodsugarRecord.getUserId());
     	if(StringUtils.isEmpty(tenantId)){
     		throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST,"患者信息不存在");
     	}
     	//血糖数据
     	List<PtBloodsugarRecord> list = newBloodSugarDao.listBloodsugar(bloodsugarRecord);
     	//处理mysql的timestemp格式问题
     	if(!CollectionUtils.isEmpty(list)){
     		for (int i = 0; i < list.size(); i++) {
     			list.get(i).setCreateTime(DateUtil.getTimeStampSec(list.get(i).getCreateTime()));
     			list.get(i).setUpdateTime(DateUtil.getTimeStampSec(list.get(i).getUpdateTime()));
     		}
     	}
     	rspMap.put("list", list);
 		return rspMap;
 	}
 	/**
 	 * 通过userid+testDate或id查询血糖数据
 	 */
 	@Override
 	public Map<String, Object> getbloodsugar(PtBloodsugarRecord bloodsugarRecord) throws Exception {
 		//创建线程安全的hashmap返回对象
     	Map<String,Object> rspMap = Collections.synchronizedMap(new HashMap<String,Object>());
     	//判断该用户是否存在
     	if(StringUtils.isEmpty(bloodsugarRecord.getId())){
     		String tenantId = weightDao.getUserTenantIdByUserId(bloodsugarRecord.getUserId());
     		if(StringUtils.isEmpty(tenantId)){
         		throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_PATIENT_NOT_EXIST,"患者信息不存在");
         	}
     	}
     	//血糖数据
     	List<PtBloodsugarRecord> list = newBloodSugarDao.listBloodsugar(bloodsugarRecord);
     	if(CollectionUtils.isEmpty(list)){
    		rspMap.put("isBack", false);
     	}else{
     		//处理mysql的timestemp格式问题
     		for (int i = 0; i < list.size(); i++) {
     			list.get(i).setCreateTime(DateUtil.getTimeStampSec(list.get(i).getCreateTime()));
     			list.get(i).setUpdateTime(DateUtil.getTimeStampSec(list.get(i).getUpdateTime()));
         	}
     		rspMap.put("isBack", true);
     	}
     	rspMap.put("list", list);
 		return rspMap;
 	}
}
